from tortoise import Tortoise
from tortoise.query_utils import Q

from app.model import mysql


async def create_media_comment(user_id: int, media_id: int, reply_revert_comment_id: int = 0, text: str = ''):
    """
    创建媒体评论
    :param user_id: 评论人编号
    :param media_id: 媒体编号
    :param reply_revert_comment_id: 被回复评论编号
    :param text: 评论正文
    :return:
    """
    reply_revert_comment_user_id = 0
    stair_reply_comment_id = 0
    if reply_revert_comment_id:
        reply_comment = await mysql.Comment.get_or_none(id=reply_revert_comment_id)
        if reply_comment:
            reply_revert_comment_user_id = reply_comment.user_id
            stair_reply_comment_id = reply_comment.stair_reply_comment_id or reply_comment.id  # 被回复评论无一级评论，则此评论就是一级评论

    comment = await mysql.Comment.create(
        object_id=media_id,
        user_id=user_id,
        text=text,
        reply_revert_comment_id=reply_revert_comment_id,
        reply_revert_comment_user_id=reply_revert_comment_user_id,
        stair_reply_comment_id=stair_reply_comment_id,
        object_comment_id=0,
    )
    conn = Tortoise.get_connection("default")
    if reply_revert_comment_id:
        # 回复者是媒体所属者，则更新标记，并更新被评论数
        media = await mysql.Media.get_or_none(id=media_id)
        if media and media.user_id == user_id:
            await conn.execute_query(
                f'UPDATE {mysql.Comment.Meta.table} SET comment_number = comment_number + 1, object_comment_id = {comment.id} WHERE id = {reply_revert_comment_id}')
        else:
            await conn.execute_query(
                f'UPDATE {mysql.Comment.Meta.table} SET comment_number = comment_number + 1 WHERE id = {reply_revert_comment_id}')
    # 更新媒体评论个数
    await conn.execute_query(
        f'UPDATE {mysql.Media.Meta.table} SET comment_number = comment_number + 1 WHERE id = {media_id}')
    if stair_reply_comment_id and stair_reply_comment_id != reply_revert_comment_id:
        # 更新一级评论的回复数
        await conn.execute_query(
            f'UPDATE {mysql.Comment.Meta.table} SET comment_number = comment_number + 1 WHERE id = {stair_reply_comment_id}')
    return comment


async def media_root_comments(media_id: int, offset: int = 0, limit: int = 10):
    """
    获取媒体的一级评论
    :param media_id:
    :param offset:
    :param limit:
    :return:
    """
    root_comments = await mysql.Comment.filter(object_id=media_id, stair_reply_comment_id=0).order_by('-created_time')\
        .offset(offset).limit(limit)
    return root_comments


async def get_comment_object_first_comment(comment_id):
    """
    获取评论的up主最新的一条回复
    :param comment_id:
    :return:
    """
    return await mysql.Comment.filter(stair_reply_comment_id=comment_id).order_by('-created_time').first()


async def is_like_comment(user_id: int, comment_id: int):
    """
    判断是否点赞评论
    :param user_id:
    :param comment_id:
    :return:
    """
    count = await mysql.CommentLike.filter(user_id=user_id, comment_id=comment_id).count()
    return count > 0


async def get_comment_children(comment_id: int, offset: int = 0, limit: int = 10, exclude_ids: list = []):
    """
    获取评论二级评论列表
    :param comment_id:
    :param offset:
    :param limit:
    :param exclude_ids:
    :return:
    """
    comments = await mysql.Comment.filter(Q(stair_reply_comment_id=comment_id, id__not_in=exclude_ids))\
        .order_by('-created_time').offset(offset).limit(limit)
    return comments


async def get_comment(comment_id):
    """
    获取单条评论
    :param comment_id:
    :return:
    """
    return await mysql.Comment.get_or_none(id=comment_id)
